package array

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2cv1c/
// 大数加一
// 算法: 直接遍历
func PlusOne(digits []int) []int {
	retArr := make([]int, len(digits))
	needPlus := false
	digits[len(digits)-1]++
	for index := len(digits) - 1; index >= 0; index-- {
		if needPlus {
			retArr[index] = digits[index] + 1
		} else {
			retArr[index] = digits[index]
		}
		if retArr[index] >= 10 {
			retArr[index] -= 10
			needPlus = true
		} else {
			needPlus = false
		}
	}
	if needPlus {
		tempRetArr := make([]int, 0)
		tempRetArr = append([]int{1}, retArr...)
		retArr = tempRetArr
	}
	return retArr
}
